package com.ken.mall.webconfig;

import com.ken.mall.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter{

    /**
     * 拦截器加载的时间点在springcontext之前，所以在拦截器中注入为null
     * 使用bean注解提前加载
     * @return
     */
    @Bean
    public HandlerInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }
    /**
     * 注册 拦截器
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}
